package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.enumeration.OperationType;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * ClassName: SetmealMapper
 * Package: com.sky.mapper
 * Description:
 *
 * @Author 谢依雯
 * @Create 2024/11/15 13:28
 */
@Mapper
public interface SetmealMapper {
    /**
     * 根据id查询套餐数量，判断套餐是否关联了菜品分类
     * @param id
     * @return
     */
    @Select("select count(*) from setmeal where category_id=#{id}")
    Integer count(Integer id);

    /**
     * 根据id修改套餐：菜品停售则关联的套餐停售、修改套餐
     * @param setmeal
     */
    @AutoFill(OperationType.UPDATE)
    void update(Setmeal setmeal);

    /**
     * 根据套餐信息动态查询所有套餐
     * @param setmeal
     * @return
     */
    List<Setmeal> list(Setmeal setmeal);

    /**
     * 根据套餐id查询包含的菜品
     * @param id
     * @return
     */
    @Select("select sd.name,sd.copies,d.image,d.description from setmeal_dish sd " +
            "left join dish d on sd.dish_id = d.id" +
            " where sd.setmeal_id=#{id}")
    List<DishItemVO> getDishItemById(Long id);

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    Page<SetmealVO> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO);

    /**
     * 新增套餐
     * @param setmeal
     */
    @AutoFill(OperationType.INSERT)
    @Insert("insert into setmeal (category_id, name, price, status,description, image, create_time, update_time, create_user, update_user) VALUES " +
            "(#{categoryId},#{name},#{price},#{status},#{description},#{image},#{createTime},#{updateTime},#{createUser},#{updateUser})" )
    @Options(useGeneratedKeys = true,keyProperty = "id")
    void insert(Setmeal setmeal);
    /**
     * 根据id查询套餐和关联的所有菜品
     * @param id
     * @return
     */
    SetmealVO getByIdWithDish(Long id);

    /**
     *根据id查询套餐信息
     * @param id
     * @return
     */
    @Select("select * from setmeal where id=#{id}")
    Setmeal getByid(Long id);

    /**
     * 根据id删除套餐中的数据
     * @param id
     */
    @Delete("delete from setmeal where id=#{id}")
    void deleteByid(Long id);

    Integer countByMap(Map map);
}
